<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="${bp}/layui/css/layui.css" media="all">
    <script src="${bp}/layui/layui.js" charset="utf-8"></script>
    <link rel="stylesheet" href="${bp}/layui/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">

    <div class="layui-form-item">
        <label class="layui-form-label required">小区名称</label>
        <div class="layui-input-block">
            <input type="text" name="addName" lay-verify="required" lay-reqtext="小区名称不能为空" placeholder="请输入小区名称" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">楼号</label>
        <div class="layui-input-block">
            <input type="text" name="addHouse" lay-verify="required" lay-reqtext="楼号不能为空" placeholder="请输入楼号" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">单元号</label>
        <div class="layui-input-block">
            <input type="text" name="addUnit" lay-verify="required" placeholder="请输入单元号" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">住户数量</label>
        <div class="layui-input-block">
            <input type="text" name="addCount" lay-verify="required" placeholder="请输入住户数量" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认添加</button>
        </div>
    </div>
</div>

<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        //监听提交
        form.on('submit(saveBtn)', function (data) {

            // 获取表单数据
            let  dt = JSON.stringify(data.field)    // {username:"tom",age:20,sex:"男"}  =>  {"username":"tom","age":20,"sex":"男"}
            // 发送异步请求添加用户信息
            $.ajax({
                url:'${bp}/user/add',
                data:dt,  //  json格式数据  {username:'lisi'}
                type:'POST',
                dataType:'JSON',
                contentType:'application/json;charset=utf-8',  //  声明发送数据的格式是json格式
                success:function (res) {
                    if(res.code == 0){
                        layer.msg(res.msg,{icon:1},function () {
                            // 关闭弹出层
                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                            // 刷新父页面
                            parent.location.reload();
                        })
                    }else{  // 添加失败
                        layer.msg(res.msg,{icon:2})
                    }
                }
            })
            return false;
        });

    });
</script>
</body>
</html>
